﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TCHSHue.DAL;

namespace TCHSHue.BLL
{
    public class HoSoBLL
    {
        internal static List<HoSo> getList(string hoTen, string soBN, string soTo, string soThua, string phuong, int pSize, int currentPage)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                if (phuong == "Tất cả")
                {
                    var ls = (from e in data.HoSos
                              where (!string.IsNullOrEmpty(hoTen) ? e.hoTen.Contains(hoTen) : true) &&
                              (!string.IsNullOrEmpty(soBN) ? e.maBN.Contains(soBN) : true) &&
                              (!string.IsNullOrEmpty(soTo) ? e.soTo.Contains(soTo) : true) &&
                              (!string.IsNullOrEmpty(soThua) ? e.soThua.Contains(soThua) : true)
                              orderby e.hoTen ascending, e.phuong ascending
                              select e).Skip((currentPage - 1) * pSize).Take(pSize); ;
                    return ls.ToList();
                }
                else
                {
                    var ls = (from e in data.HoSos
                              where (!string.IsNullOrEmpty(hoTen) ? e.hoTen.Contains(hoTen) : true) &&
                              (!string.IsNullOrEmpty(soBN) ? e.maBN.Contains(soBN) : true) &&
                              (!string.IsNullOrEmpty(soTo) ? e.soTo.Contains(soTo) : true) &&
                              (!string.IsNullOrEmpty(soThua) ? e.soThua.Contains(soThua) : true) &&
                              (!string.IsNullOrEmpty(phuong) ? e.phuong.Contains(phuong) : true)
                              orderby e.hoTen ascending, e.phuong ascending
                              select e).Skip((currentPage - 1) * pSize).Take(pSize); ;
                    return ls.ToList();
                }
            }
        }
        internal static int getToTal(string hoTen, string soBN, string soTo, string soThua, string phuong)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                if (string.IsNullOrEmpty(soBN))
                {
                    var ts = (from e in data.HoSos
                              where (!string.IsNullOrEmpty(hoTen) ? e.hoTen.Contains(hoTen) : true) &&
                              (!string.IsNullOrEmpty(soBN) ? e.maBN.Contains(soBN) : true) &&
                              (!string.IsNullOrEmpty(soTo) ? e.soTo.Contains(soTo) : true) &&
                              (!string.IsNullOrEmpty(soThua) ? e.soThua.Contains(soThua) : true) &&
                              (!string.IsNullOrEmpty(phuong) ? e.phuong.Contains(phuong) : true)
                              orderby e.hoTen ascending, e.phuong ascending
                              select e).Count();
                    return ts;
                }
                else
                    return 0;
            }
        }

        internal static HoSo getHoSo(string maBN)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                return (from e in data.HoSos
                        where maBN == e.maBN
                        select e).SingleOrDefault();
            }
        }

        internal static List<HoSo> getListByKey(string key, int pSize, int currentPage)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                if (!string.IsNullOrEmpty(key))
                {
                    var ls = (from e in data.HoSos
                              where (e.hoTen.Contains(key) ||
                              e.maBN.Contains(key) ||
                               e.soTo.Contains(key) ||
                             e.soThua.Contains(key) ||
                            e.phuong.Contains(key))
                              orderby e.hoTen ascending, e.phuong ascending
                              select e).Skip((currentPage - 1) * pSize).Take(pSize); ;
                    return ls.ToList();
                }
                else
                    return null;
            }
        }
        internal static int getTotalByKey(string key)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                if (!string.IsNullOrEmpty(key))
                {
                    var ts = (from e in data.HoSos
                              where (e.hoTen.Contains(key) ||
                              e.maBN.Contains(key) ||
                               e.soTo.Contains(key) ||
                             e.soThua.Contains(key) ||
                            e.phuong.Contains(key))
                              select e).Count();
                    return ts;
                }
                else
                    return 0;
            }
        }
    }
}